Return to doc.sitecore.com

Valid for Sitecore 5.2, 5.3
How to request an empty field programmatically?

Q: I have a Sitecore item with an html field ”text”. The field has 2 languages, Danish and English. In English, there is some html text. In Danish, there is no text. How can I get those empty fields returned when iterating all fields programmatically? The following code does not solve the problem:

 

public void IterateAllFields(Sitecore.Data.Items.Item item)

{

  foreach (Sitecore.Data.Fields.Field field in item.Fields)

  {

    if (fld.CanRead && fld.CanWrite)

    {

      ... output field title: fld.Title

      ... output more fields

    }

  }

}

 

Text fields are not included when executing this code in Danish context…

 

A: In current build of Sitecore such a behavior is implemented for the sake of performance.

 

But there is a nice workaround: there is a method ReadAll() in the Fields collection, which makes the empty fields writable from API. See the sample code below:

 

      item.Fields.ReadAll();

      for (int i = 0; i < item.Fields.Count; i++)

      {

         Response.Write(item.Fields[i].Title+": ");

         Response.Write(item.Fields[i].Value);

         Response.Write("<br/>");

      }